package com.zzzyh.forumsystemclient.cotroller;

import com.alibaba.fastjson.JSON;
import com.zzzyh.forumsystemclient.common.AppResult;
import com.zzzyh.forumsystemclient.model.Article;
import com.zzzyh.forumsystemclient.model.LoginUser;
import com.zzzyh.forumsystemclient.model.PageResult;
import com.zzzyh.forumsystemclient.model.Resource;
import com.zzzyh.forumsystemclient.services.IResourceService;
import com.zzzyh.forumsystemclient.utils.FileUtils;
import com.zzzyh.forumsystemclient.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/resource")
@Slf4j
public class ResourceController {

    @Autowired
    private IResourceService resourceService;

    @RequestMapping("/list")
    public AppResult<PageResult<Resource>> list(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                                @RequestParam(required = false) Integer category,
                                                @RequestParam(required = false) String name){
        if(category != null){
            if (name ==  null){
                List<Resource> resources = resourceService.listByCategory(pageNum, pageSize, category);
                Integer total = resourceService.totalByCategoryId(category);
                return AppResult.success(new PageResult<>(pageNum, pageSize, total, resources));
            }else {
                List<Resource> resources = resourceService.listByCategoryName(pageNum, pageSize, category,name);
                Integer total = resourceService.totalByCategoryIdAndName(category, name);
                return AppResult.success(new PageResult<>(pageNum, pageSize, total, resources));
            }

        }else{
            return AppResult.failed("参数错误");
        }

    }

    @RequestMapping("/add")
    public AppResult<String> add(@RequestBody Resource resource,
        @RequestHeader("Authorization") String token){
        LoginUser user = JSON.parseObject(JwtUtils.getSubjectFromToken(token), LoginUser.class);
        resource.setUserId(user.getUser().getId());
        resource.setCreateTime(new Date());
        resource.setIsFree(0);
        resource.setStatus(2);
        resource.setDownloads(0);
        resource.setView(0);
        log.info("资源信息为：{}", resource);
        resourceService.add(resource);
        return AppResult.success("添加成功");
    }

    @PostMapping("/uploadFile")
    public AppResult<String> uploadFile(@RequestParam("file") MultipartFile file) {
        // 处理文件上传逻辑
        try {
            // 调用上传工具方法
            String fileUrl = FileUtils.uploadUtils(file, "resource");
            log.info("上传成功: {}", fileUrl);
            return AppResult.success("上传成功", fileUrl);
        } catch (Exception e) {
            log.error("上传失败", e);
            return AppResult.failed("上传失败: " + e.getMessage());
        }
    }

    @RequestMapping("/increaseDownload/{resourceId}")
    public AppResult<String> increaseDownload(@PathVariable Integer resourceId){
        resourceService.increaseDownload(resourceId);
        return AppResult.success("新增成功");
    }

    @RequestMapping("/increaseView/{resourceId}")
    public AppResult<String> increaseView(@PathVariable Integer resourceId){
        resourceService.increaseView(resourceId);
        return AppResult.success("新增成功");
    }
}
